package fancyfoods.rest;
import static com.google.inject.Guice.createInjector;
import static org.ops4j.peaberry.Peaberry.osgiModule;

import org.ops4j.peaberry.Export;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;

import com.google.inject.Inject;

import fancyfoods.food.Food;
import fancyfoods.food.Inventory;

public class Activator implements BundleActivator {

	@Inject
	Export<Inventory> handle;

	public void start(final BundleContext ctx) throws Exception {
		ServiceReference ref = ctx.getServiceReference(LogService.class.getName());
		LogService log = null;
        if (ref != null)
        {
            log = (LogService) ctx.getService(ref);
        }
		// export service and inject handle into the activator
		createInjector(osgiModule(ctx), new Config()).injectMembers(this);
		
		Food food = handle.get().getFood("cheese");
		
		log.log(LogService.LOG_DEBUG, "Food name is : " + food.getName() + " from Peaberry Service");
	}

	public void stop(final BundleContext ctx) throws Exception {
		handle.unput();
	}
	
}